package com.yfbao.horizon.schedule.tk.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Slf4j
@Component
@Order(Integer.MAX_VALUE)
public class MyCacheEvictAspect {

//    @Before("execution(* org.springframework.cache.Cache.evict(..))")
//    public void beforeYourMethod(JoinPoint joinPoint) {
//        // 获取方法参数
////        joinPoint.
//        Object[] args = joinPoint.getArgs();
//
//        // 在这里你可以处理参数，打印日志，等等
//        System.out.println("Before method execution. Method arguments: " + Arrays.toString(args));
//    }
//
//    @AfterReturning(value = "execution(* org.springframework.cache.Cache.evict(..))",returning = "result")
//    public void afterYourMethod(Object result) {
//        // 在方法正常返回后执行的逻辑
//        System.out.println("After method execution. Method returned: " + result);
//    }

    @Before("bean(*RedisCache) && execution(public * evict(..)) && args(key)")
    public void beforeCacheEvict(JoinPoint joinPoint, Object key) {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before Cache.evict method. Key: " + key);
    }

    @Before("execution(* org.springframework.cache.Cache.evict(..))")
    public void beforeCacheEvict2(JoinPoint joinPoint) {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before Cache.evict method2. Key: " );
    }

    @Before("execution(* org.springframework.cache.Cache.evict(String)) && args(key)")
    public void beforeCacheEvict3(JoinPoint joinPoint, Object key) {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before Cache.evict method3. Key: " + key);
    }

    @Before("execution(* org.springframework.data.redis.cache.RedisCache.evict(..))")
    public void beforeCacheEvict4(JoinPoint joinPoint) {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before Cache.evict method4. Key: " );
    }

    @Before("execution(* com.yfbao.horizon.schedule.tk.service.inter.MyTest.test(..))")
    public void beforeCacheEvict5(JoinPoint joinPoint) {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before MyTest.test method5. Key:");
    }


    @Around("execution(* com.yfbao.horizon.schedule.tk.testService.MyTestServiceImpl.test(..))")
    public Object  beforeCacheEvict6(ProceedingJoinPoint joinPoint) throws Throwable {
        // 在 Cache.evict 方法执行前执行的逻辑
        log.info("Before MyTest.test method6. Key:");
        return  joinPoint.proceed();
    }

}
